using MbUnit.Framework;
using EnterpriseLibraryContrib.PostSharp4EntLib.PolicyInjection;

namespace EnterpriseLibraryContrib.PostSharp4EntLib.Test.PolicyInjection.AttributeDrivenPolicies
{
    [TestFixture]
    public class Caching
    {
        private int cachedMethodCallNumber = 0;
        private static int staticCachedMethodCallNumber = 0;

        [CachingCallHandler]
        private int CachedMethod()
        {
            return this.cachedMethodCallNumber++;
        }

        [CachingCallHandler]
        private static int StaticCachedMethod()
        {
            return staticCachedMethodCallNumber++;
        }

        [Test]
        public void TestCachedMethod()
        {
            Assert.AreEqual(this.CachedMethod(), this.CachedMethod());
        }

        [Test]
        public void TestStaticCachedMethod()
        {
            Assert.AreEqual(StaticCachedMethod(), StaticCachedMethod());
        }

        [CachingCallHandler]
        private static void MethodWithOut(out int value)
        {
            value = 5;
        }

        [Test]
        public void TestMethodWithOut()
        {
            int value;
            MethodWithOut(out value);
            Assert.AreEqual(5, value, "incorrect: from real method");

            MethodWithOut(out value);
            Assert.AreEqual(5, value, "incorrect: from cache");
        }

        [CachingCallHandler]
        public static void MethodWithRef(ref int value)
        {
            value++;
        }

        [Test]
        public void TestMethodWithRef()
        {
            int value = 4;
            MethodWithRef(ref value);
            Assert.AreEqual(5, value, "incorrect: from real value");

            value = 4;
            MethodWithRef(ref value);
            Assert.AreEqual(5, value, "incorrect: from cache");

            
        }
    }
}